<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button size="small" type="primary" @click="addDialogVisible = true"
          >添加后台资源</el-button
        >
      </div>
      <el-table
        border
        stripe
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="small"
      >
        <el-table-column prop="categoryId" label="资源分类ID" min-width="100" />
        <el-table-column prop="name" label="资源名称" min-width="150" />

        <el-table-column prop="createTime" label="创建时间" min-width="150" />
        <el-table-column prop="url" label="资源地址" min-width="150" />
        <el-table-column prop="description" label="资源描述" min-width="150" />
        <el-table-column label="操作" min-width="120" fixed="right">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              type="warning"
              size="small"
              @click="onEdit(scope.row.id)"
            ></el-button>
            <el-button
              icon="el-icon-delete"
              type="danger"
              size="small"
              @click="handleDelete(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加 -->
    <el-dialog
      title="添加后台资源"
      :visible.sync="addDialogVisible"
      @close="addRoleDialogClosed"
      width="50%"
    >
      <el-form
        :model="addRuleForm"
        :rules="addRules"
        ref="addRuleFormRef"
        label-width="120px"
      >
        <el-form-item label="资源分类 ：" prop="categoryId">
          <el-select v-model="addRuleForm.categoryId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <!-- 身份名称 -->
        <el-form-item label="资源名称 ：" prop="name">
          <el-input v-model="addRuleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="是否商户菜单 :" prop="isMer">
          <el-switch
            v-model="addRuleForm.isMer"
            active-color="#13ce66"
            inactive-color="#708090"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="资源地址 ：" prop="url">
          <el-input v-model="addRuleForm.url"></el-input>
        </el-form-item>
        <el-form-item label="资源描述 ：" prop="description">
          <el-input
            type="textarea"
            v-model="addRuleForm.description"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDialogSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改 -->
    <el-dialog
      title="修改后台资源"
      :visible.sync="editDialogVisible"
      @close="editRoleDialogClosed"
      width="50%"
    >
      <el-form
        :model="editRuleForm"
        :rules="addRules"
        ref="editRuleFormRef"
        label-width="120px"
      >
        <el-form-item label="资源分类 ：" prop="categoryId">
          <el-select v-model="editRuleForm.categoryId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资源名称 ：" prop="name">
          <el-input v-model="editRuleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="是否商户菜单 :" prop="isMer">
          <el-switch
            v-model="editRuleForm.isMer"
            active-color="#13ce66"
            inactive-color="#708090"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="资源地址 ：" prop="url">
          <el-input v-model="editRuleForm.url"></el-input>
        </el-form-item>
        <el-form-item label="资源描述 ：" prop="description">
          <el-input
            type="textarea"
            v-model="editRuleForm.description"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getResourceList,
  createResourceApi,
  deleteResourceApi,
  getCategoryList,
  editCategoryApi,
  infoResourceApi,
} from "@/api/setting";
export default {
  name: "SystemLog",
  data() {
    return {
      // 添加后台菜单对话框
      addDialogVisible: false,
      editDialogVisible: false,
      isChecked: false,
      listLoading: true,
      tableData: {
        data: [],
        total: 0,
      },
      addRuleForm: {},
      editRuleForm: {},
      timeVal: [],
      section_time: [],
      importanceOptions: ["POST", "DELETE"],

      options: [],
      addRules: {
        name: [{ required: true, message: "请输入资源名称", trigger: "blur" }],
        description: [
          { required: true, message: "请输入资源描述", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getCategory();
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 列表
    getList() {
      getResourceList()
        .then((res) => {
          console.log(res);
          this.tableData.data = res.data;
          this.tableData.total = res.data.length;
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    // 资源分类列表
    getCategory() {
      getCategoryList()
        .then((res) => {
          this.options = res.data;
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },

    // 编辑
    onEdit(id) {
      this.editDialogVisible = true;
      infoResourceApi(id)
        .then((res) => {
          console.log(res);
          this.editRuleForm = res.data;
          console.log(this.editRuleForm);
        })
        .catch((message) => {
          console.log(message);
        });
    },
    // 编辑对话框确认按钮点击事件
    editDialogSubmit() {
      console.log(this.editRuleForm, "54545");
      editCategoryApi(this.editRuleForm.id, this.editRuleForm)
        .then((res) => {
          console.log(res);
          this.editDialogVisible = false;
          this.$message.success("资料修改成功");
          this.getList();
        })
        .catch((res) => {
          console.log(res);
          this.editDialogVisible = false;
          this.$message.error("资料修改失败！");
        });
    },
    // 删除按钮点击事件
    async handleDelete(id) {
      console.log(id);
      // 弹框询问是否删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该资源, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      // 如果用户确认了删除，则返回值为字符串：confirm
      // 如果用户取消了删除，则返回值为字符串：cancel
      console.log(confirmResult);
      if (confirmResult !== "confirm") {
        this.$message.info("已经取消了删除");
      } else {
        deleteResourceApi(id)
          .then(() => {
            this.$message.success("删除资源成功");
            // 删除成功后，更新数据
            this.getList();
          })
          .catch(() => {
            this.$message.error("删除资源失败！");
          });
      }
    },

    // 添加对话框确认按钮
    addDialogSubmit() {
      this.$refs.addRuleFormRef.validate((valid) => {
        if (!valid) return;
        createResourceApi(this.addRuleForm)
          .then((message) => {
            console.log(message);
            this.addDialogVisible = false;
            this.$message.success("资源添加成功");
            this.getList();
          })
          .catch((message) => {
            console.log(message);
            this.addDialogVisible = false;
          });
      });
    },
    addRoleDialogClosed() {
      this.$refs.addRuleFormRef.resetFields();
    },
    editRoleDialogClosed() {
      this.$refs.editRuleFormRef.resetFields();
    },
  },
};
</script>

<style scoped lang="scss">
.filter-container .filter-item {
  margin-bottom: 0;
}
.el-switch {
  width: 40px;
}
::v-deep.el-input--medium .el-input__inner {
  line-height: 32px;
  height: 32px;
}
</style>
